package com.alks.function.controller.pcFactoryQuery;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.enums.CompanyEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.pcfactoryquery.PcMtlAccDto;
import com.alks.function.data.request.factoryquery.AcMtlAccHeadRequest;
import com.alks.function.service.pcfactoryquery.IPcMtlAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiRestController("/pcFactorQuery")
@Api(value = "总经办", tags = "总经办-工厂查询模块-材料库存查询")
@Slf4j
@RoleCheck(prgIds = {"100203","100411","140206","150207","160206","170507","110306","120203","170703","180603","180709","190206","210205"})
public class MtlStkQueryController {
    @Autowired
    private IPcMtlAccService accService;
    @PutMapping("/queryMtlAccHead")
    @ApiOperation(value = "材料库存-材料库存-分页条件查询",tags = "总经办-工厂查询模块-材料库存查询")
    @WebLogger
    public ResponseInfo queryMtlAccHead(@RequestBody AcMtlAccHeadRequest request){
        Map<String, Object> map = accService.queryMtlAccHead(request);
        return ResponseInfo.ok(map);
    }

    @GetMapping("/queryMtlDetail/{mtlNo}")
    @ApiOperation(value = "材料库存-出入库明细-条件查询",tags = "总经办-工厂查询模块-材料库存查询")
    @WebLogger
    public ResponseInfo queryMtlAccDetail(@PathVariable String mtlNo){
        return accService.queryMtlAccDetail(mtlNo);
    }

    @PutMapping("/mtlAcc/export")
    @ApiOperation(value = "导出",tags = "总经办-工厂查询模块-材料库存查询")
    @WebLogger
    public void mtlAccExport(@RequestBody AcMtlAccHeadRequest request, HttpServletResponse response){
        /*获取数据*/
        String templetName = "材料库存明细模版.xlsx";
        String excelName = "材料库存明细"+ DateUtils.dateTimeNow();
        request.setPageSize(-1);
        request.setPageNum(-1);
        Map<String, Object> map = accService.queryMtlAccHead(request);
        Map<String,Object> date = new HashMap<>(2);
        date.put("company", CompanyEnum.getCompanyName(UserIdThread.get().getCompanyId()));
        date.put("now", LocalDateTime.now());

        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, (List<PcMtlAccDto>) map.get("dtos"), date);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }

}
